package com.ymatou.iapi.godmch.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.context.annotation.Bean;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.removeCSBean;
import com.ymatou.iapi.godmch.service.removeCSCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.cs_info;
import com.ymttest.database.model.godmch.buyer_invite_code;
import com.ymttest.database.model.godmch.buyer_invite_codeExample;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.database.sqlwapper.godmch.buyer_invite_codeWapper;
import com.ymttest.database.sqlwapper.godmch.cs_infoWapper;
/**
*移除客服
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_removeCS{
	private static removeCSBean removecsBean;
	private static removeCSCall removecsCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("移除客服");
	}

	@Before
	public void caseUp() {
		removecsBean=new removeCSBean();
		removecsCall=new removeCSCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(removecsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_removeCS_001() {
		Logger.start(true,"移除客服,验证商户id与客服id匹配,返回成功");
		try {
			Integer templateId=1;
			Integer mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        removecsBean.setCsId(csId);
	        removecsBean.setMchId(mchId);
			removecsCall.setData(removecsBean);
			removecsCall.callService();			
			Logger.verifyEquals("200", removecsCall.getString("status"), "验证status");
			cs_info csi = new DefGodMchWapper().selectCsInfo(mchId, csId).get(0);
			Logger.verifyEquals(removecsBean.getCsId(), csi.getCsId(), "验证csId");
			Logger.verifyEquals(removecsBean.getMchId(), csi.getMchId(), "验证mchId");
			Logger.verifyEquals(-1, csi.getState(), "验证State");
			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("remove_cs-"+mchId+"-"+csId,"cs_remove");
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
//			Logger.verifyEquals(mchId.toString(), joa.get(0).get("mchId").toString(), "验证总线消息mchId");
			Logger.verifyEquals(csId.toString(), joa.get(0).get("userId").toString(), "验证总线消息csId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_removeCS_002() {
		Logger.start(true,"移除客服,验证商户id与客服id不匹配,返回失败");
		try {
			Integer templateId=1;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        removecsBean.setCsId(csId);
	        removecsBean.setMchId(mchId+1);
			removecsCall.setData(removecsBean);
			removecsCall.callService();			
			Logger.verifyEquals("5023", removecsCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_removeCS_003() {
		Logger.start(true,"移除客服,验证商户id为空,返回错误");
		try {
			removecsBean.setCsId(1);
	        removecsBean.setMchId(null);
			removecsCall.setData(removecsBean);
			removecsCall.callService();			
			Logger.verifyEquals("4001", removecsCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_removeCS_004() {
		Logger.start(true,"移除客服,验证客服id为空,返回错误");
		try {
			removecsBean.setCsId(null);
	        removecsBean.setMchId(1);
			removecsCall.setData(removecsBean);
			removecsCall.callService();			
			Logger.verifyEquals("4001", removecsCall.getString("status"), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_removeCS_005() {
		Logger.start(true,"重复移除客服,失败");
		try {
			Integer templateId=1;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        removecsBean.setCsId(csId);
	        removecsBean.setMchId(mchId);
			removecsCall.setData(removecsBean);
			removecsCall.callService();			
			removecsCall.callService();		
			Logger.verifyEquals("5023", removecsCall.getString("status"), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_removeCS_006() {
		Logger.start(true,"移除客服,验证客服有邀请码,返回成功,邀请码同时也删除");
		try {

	        removecsBean.setCsId(501031356);
	        removecsBean.setMchId(3383);
			removecsCall.setData(removecsBean);
			
			//邀请码
			buyer_invite_codeExample example=new buyer_invite_codeExample();
			example.createCriteria().andUserIdEqualTo(removecsBean.getCsId());
			buyer_invite_codeWapper codeMapper=new buyer_invite_codeWapper();
			buyer_invite_code code=codeMapper.selectByExample(example).get(0);	
			code.setCodeStatus(0);
			Integer result=codeMapper.updateByPrimaryKey(code);
			//客服
			DefGodMchWapper DefGodMchWapper=new DefGodMchWapper();
			cs_infoWapper cs_infoWapper=new cs_infoWapper();
			cs_info csi = DefGodMchWapper.selectCsInfo(removecsBean.getMchId(), removecsBean.getCsId()).get(0);
			csi.setState(0);
			cs_infoWapper.updateByPrimaryKey(csi);
			
			removecsCall.callService();			

			code=codeMapper.selectByExample(example).get(0);	
			Logger.verifyEquals("200", removecsCall.getString("status"), "验证status");
			Logger.verifyEquals(-1,code.getCodeStatus(), "验证CodeStatus=-1删除");
			
			csi = new DefGodMchWapper().selectCsInfo(removecsBean.getMchId(), removecsBean.getCsId()).get(0);
			Logger.verifyEquals(removecsBean.getCsId(), csi.getCsId(), "验证csId");
			Logger.verifyEquals(removecsBean.getMchId(), csi.getMchId(), "验证mchId");
			Logger.verifyEquals(-1, csi.getState(), "验证State");
			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("remove_cs-"+removecsBean.getMchId()+"-"+removecsBean.getCsId(),"cs_remove");
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
//			Logger.verifyEquals(mchId.toString(), joa.get(0).get("mchId").toString(), "验证总线消息mchId");
			Logger.verifyEquals(removecsBean.getCsId().toString(), joa.get(0).get("userId").toString(), "验证总线消息csId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}